﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using VideoCache.Server.BLL;
using VideoCache.Server.Model;
using VideoCache.Spider;

namespace VideoCache.Server.MVCWeb.Areas.Admin.Models
{
    public class AlbumModel
    {
        public int Id { get; set; }

        [Display(Name = "视频名称")]
        [Required(ErrorMessage = "请输入视频名称。")]
        [Remote("IsAlbumTitleAvaliable", "Album", "Admin", ErrorMessage = "视频名称已存在", AdditionalFields = "Id", HttpMethod = "POST")]
        [StringLength(200, MinimumLength = 1, ErrorMessage = "视频标题不能多于200个字符。")]
        public string Title { get; set; }

        [Display(Name = "类别")]
        [Required(ErrorMessage = "请选择视频所属类别。")]
        public int TypeId { get; set; }

        [Display(Name = "地区")]
        [Required(ErrorMessage = "请选择视频上映地区。")]
        public int AreaId { get; set; }

        [Display(Name = "导演")]
        [Required(ErrorMessage = "请输入导演。")]
        [StringLength(200, MinimumLength = 1, ErrorMessage = "导演不能多于200个字符。")]
        public string Directors { get; set; }

        [Display(Name = "主演")]
        [Required(ErrorMessage = "请输入主演。")]
        [StringLength(200, MinimumLength = 1, ErrorMessage = "主演不能多于200个字符。")]
        public string Actors { get; set; }

        [Display(Name = "上映年份")]
        [Required(ErrorMessage = "请输入上映年份。")]
        [Range(1970, 2100, ErrorMessage = "上映年份必须在1970-2100之间。")]
        public int PubYear { get; set; }

        [Display(Name = "优先级")]
        [Required(ErrorMessage = "请输入优先级。")]
        [Range(0, 10, ErrorMessage = "优先级必须为0-10的数字。")]
        public int Priority { get; set; }

        [Display(Name = "评分")]
        [Required(ErrorMessage = "请输入评分。")]
        [Range(0, 100, ErrorMessage = "评分只能是0-100的数字。")]
        public int Rating { get; set; }

        [Display(Name = "百度编号")]
        [Required(ErrorMessage = "请输入百度编号。")]
        public int BaiduId { get; set; }

        [Display(Name = "是否更新结束")]
        [Required(ErrorMessage = "请选择是否更新结束。")]
        public bool IsEnd { get; set; }

        [Display(Name = "状态")]
        [Required(ErrorMessage = "请选择状态。")]
        public int Status { get; set; }

        [Display(Name = "视频简介")]
        [Required(ErrorMessage = "请输入上映年份。")]
        [DataType(DataType.MultilineText)]
        public string Description { get; set; }

        [Display(Name = "创建时间")]
        public DateTime CreateTime { get; set; }

        [Display(Name = "最后修改时间")]
        public DateTime ModifyTime { get; set; }

        public AlbumModel()
        {
            this.Priority = 5;
        }

        public AlbumModel(int id)
        {
            AlbumInfo info = AlbumBLL.Find(id);

            if (info == null) throw new ArgumentException("指定的视频编号不存在。");

            this.Id = info.Id;
            this.Title = info.Title;
            this.TypeId = info.TypeId;
            this.AreaId = info.AreaId;
            this.Directors = info.Director;
            this.Actors = info.Actor;
            this.Status = info.Status;
            this.Rating = info.Rating;
            this.IsEnd = info.IsEnd > 0;
            this.Priority = info.Priority;
            this.PubYear = info.PubYear;
            this.BaiduId = info.BaiduId;
            this.CreateTime = info.CreateTime;
            this.ModifyTime = info.ModifyTime;

            AlbumDescriptionInfo descInfo = AlbumDescriptionBLL.Find(id);

            if (descInfo != null) this.Description = descInfo.Description;
        }

        internal void Save()
        {
            AlbumInfo info = new AlbumInfo();

            info.Id = this.Id;
            info.Title = this.Title;
            info.TypeId = this.TypeId;
            info.AreaId = this.AreaId;
            info.Director = this.Directors;
            info.Actor = this.Actors;
            info.Status = this.Status;
            info.Rating = this.Rating;
            info.IsEnd = this.IsEnd ? (ulong)1 : 0;
            info.Priority = this.Priority;
            info.PubYear = this.PubYear;
            info.BaiduId = this.BaiduId;

            AlbumDescriptionInfo description = new AlbumDescriptionInfo();

            description.AlbumId = info.Id;
            description.Description = this.Description;

            if (info.Id > 0)
            {
                AlbumBLL.EditAlbum(info, description);
            }
            else
            {
                AlbumBLL.CreateAlbum(info, description);
            }
        }

        public static AlbumModel[] SearchFromBaiduVideo(string keyword)
        {
            var list = SpiderFactory.SearchAlbum(keyword);
            List<AlbumModel> models = new List<AlbumModel>();
            foreach (var item in list)
            {
                models.Add(TranslateAlbum(item));
            }

            return models.ToArray();
        }

        private static AlbumModel TranslateAlbum(BaiduAlbum album)
        {
            AlbumModel model = new AlbumModel();
            model.Title = album.title;

            // 组织视频名称

            if (!string.IsNullOrEmpty(album.season) && album.season != "0")
            {
                model.Title += " 第" + album.season + "部";
            }

            if (!string.IsNullOrEmpty(album.language))
            {
                model.Title += " " + album.language;
            }

            model.IsEnd = album.finish == "1";
            model.Rating = string.IsNullOrEmpty(album.rating) ? 0 : ((int)Convert.ToDouble(album.rating) * 10);
            model.Description = album.brief;
            model.BaiduId = Convert.ToInt32(album.id);

            switch (album.ty)
            {
                case "21":
                    model.TypeId = 2;
                    break;
                case "22":
                    model.TypeId = 3;
                    break;
            }

            if (album.area != null && album.area.Length > 0)
            {
                if (album.area[0].IndexOf("内地") > -1 || album.area[0].IndexOf("大陆") > -1)
                {
                    model.AreaId = 7;
                }
                else if (album.area[0].IndexOf("香港") > -1)
                {
                    model.AreaId = 8;
                }
                else if (album.area[0].IndexOf("台湾") > -1)
                {
                    model.AreaId = 9;
                }
                else if (album.area[0].IndexOf("日本") > -1)
                {
                    model.AreaId = 10;
                }
                else if (album.area[0].IndexOf("韩国") > -1)
                {
                    model.AreaId = 11;
                }
                else if (album.area[0].IndexOf("美国") > -1)
                {
                    model.AreaId = 12;
                }
                else
                {
                    model.AreaId = 13;
                }
            }

            if (album.director != null)
            {
                foreach (string dir in album.director)
                {
                    if (!string.IsNullOrEmpty(model.Directors)) model.Directors += ",";
                    model.Directors += dir;
                }
            }

            if (album.actor != null)
            {
                foreach (string act in album.actor)
                {
                    if (!string.IsNullOrEmpty(model.Actors)) model.Actors += ",";
                    model.Actors += act;
                }
            }

            model.PubYear = string.IsNullOrEmpty(album.al_date) ? Convert.ToInt32(album.pubtime) : Convert.ToInt32(album.al_date);

            return model;
        }
    }
}